package com.qdlc.p2p.dal.model;

import org.springframework.beans.BeanUtils;

import com.qdlc.p2p.common.exception.ScoreException;
import com.qdlc.p2p.common.exception.UserException;
import com.qdlc.p2p.common.util.Page;
import com.qdlc.p2p.dal.dto.Goods;
import com.qdlc.p2p.dal.dto.ScoreConvert;


/**
 * 积分兑换信息model
 * @author xxx
 * @version 2.0
 * @date 2015年6月15日 下午5:58:27
 */
public class ScoreConvertModel extends ScoreConvert {
	
	/** 0未审核 */
    public static final byte WAIT_AUDIT = 0; 
    /** 1审核通过 */
    public static final byte PASS_AUDIT = 1;
    /** -1审核不通过 */
    public static final byte NOT_PASS_AUDIT = -1;
    /** 2已发货 */
    public static final byte IS_DELIVERY = 2;
    /** 3已收货 */
    public static final byte IS_RECEIVE = 3;

	/** 商品名称 */
    private String name;
    
    /**
	 * 是否虚拟商品，0：不是，1：是
	 */
	private int isVirtual;
    
    /** 用户名 */
    private String userName;
    
    /** 用户ID */
    private long userId;
    /** 地址主键ID */
    private long addressId;
    
    /** 商品ID */
    private long goodsId;
    
    /** 第一张图片 */
    private String imgSrc;
    
    /** 开始时间 **/
    private String startTime;
    
    /** 结束时间 **/
    private String endTime;
    
    /** 当前页数 **/
    private int page;
    
    /** 分页数 **/
    private int rows = Page.ROWS;
    
    /** 条件查询 **/
    private String searchName;

    public static ScoreConvertModel instance(ScoreConvert item) {
        ScoreConvertModel model = new ScoreConvertModel();
        BeanUtils.copyProperties(item, model);
        return model;
    }

    public ScoreConvert prototype() {
        ScoreConvert item = new ScoreConvert();
        BeanUtils.copyProperties(this, item);
        return item;
    }
    
    /**
     * 兑换用户验证
     * @param userId
     * @return
     */
    public boolean checkUser(long userId){
        if(this.getUserId() != userId || this.getUserId() <= 0){
            throw new UserException("用户信息不正确！", 1);
        }
        return true;
    }
    
    /**
     * 积分验证
     * @param userId
     * @return
     */
    public boolean checkScore(int score){
         if(score == 0 || this.getScore()*this.getConvertNum() > score){//如果总需要积分大于有效积分
            throw new ScoreException("用户积分不足！", 1);
        }
        return true;
    }
    
    /**
     * 商品验证
     * @param userId
     * @return
     */
    public boolean checkGoods(Goods item){
        if(item == null){
            throw new ScoreException("用户兑换的商品不存在！", 1);
        }
//       if(item.getStatus() != GoodsModel.PASS_AUDIT){
//            throw new ScoreException("此商品目前暂时还不能兑换！", 1);
//        }
        if(item.getType() != GoodsModel.UP_SHELVES){
            throw new ScoreException("此商品还未上架，暂时还不能兑换！", 1);
        }
        if(this.getConvertNum() <= 0 || this.getConvertNum() > 
            (item.getStore() - item.getFreezeStore() -item.getSellAcount())){
            throw new ScoreException("兑换商品库存不够！", 1);
        }
/*        if(this.getReceiveAddress() == null || this.getReceivePhone() == null || this.getReceiveUserName() == null){
            throw new ScoreException("兑换商品联系信息不正确！", 1);
        }*/
        return true;
    }

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getIsVirtual() {
		return isVirtual;
	}

	public void setIsVirtual(int isVirtual) {
		this.isVirtual = isVirtual;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public long getUserId() {
		return userId;
	}

	public void setUserId(long userId) {
		this.userId = userId;
	}

	public long getAddressId() {
		return addressId;
	}

	public void setAddressId(long addressId) {
		this.addressId = addressId;
	}

	public long getGoodsId() {
		return goodsId;
	}

	public void setGoodsId(long goodsId) {
		this.goodsId = goodsId;
	}

	public String getImgSrc() {
		return imgSrc;
	}

	public void setImgSrc(String imgSrc) {
		this.imgSrc = imgSrc;
	}

	public String getStartTime() {
		return startTime;
	}

	public void setStartTime(String startTime) {
		this.startTime = startTime;
	}

	public String getEndTime() {
		return endTime;
	}

	public void setEndTime(String endTime) {
		this.endTime = endTime;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getRows() {
		return rows;
	}

	public void setRows(int rows) {
		this.rows = rows;
	}

	public String getSearchName() {
		return searchName;
	}

	public void setSearchName(String searchName) {
		this.searchName = searchName;
	}
    
}
